<?php
/**
 * Created by PhpStorm.
 * User: xsp
 * Date: 2018/5/13
 * Time: 18:47
 */

namespace backend\controllers;

use yii\data\ActiveDataProvider;
use yii\db\Query;

class RbacController extends CommonController
{
    protected $mustLogin = ['createrole', 'roles'];

    public function actionCreaterole()
    {

        $auth = \Yii::$app;
        var_dump($auth->getAuthManager());die;
        if (\Yii::$app->request->isPost) {

            $role = $auth->createRole(null);
            $post = \Yii::$app->request->post();
            if (empty($post['name']) || empty($post['description'])) {
                throw  new \Exception('参数错误');
            }

            $role->name = $post['name'];
            $role->description = $post['description'];
            $role->ruleName = empty($post['ruleName']) ? '' : $post['ruleName'];
            $role->data = empty($post['data']) ? '' : $post['data'];


            if ($auth->add($role)) {
                \Yii::$app->session->setFlash('info', '添加成功');
            };

        }
        return $this->render('createitem');
    }

    public function actionRoles()
    {
        $auth = \Yii::$app->authManager;
//        var_dump($auth);die;
        $data = new ActiveDataProvider([
            'query' => (new Query())->from($auth->itemTable)->where(['type' => 1]),
            'pagination' => ['pageSize' => 2]
        ]);
        return $this->render('roles', ['dataProvider' => $data]);
    }
}